<template>
  <div class="caseManage">
    <el-tabs v-model="activeName" type="card" class="elTabs">
      <el-tab-pane name="first">
        <div slot="label" class="pageHeader">分配用户</div>
      </el-tab-pane>
    </el-tabs>
    <div class="container">
      <div class="searchBar" v-show="showSearch">
        <div class="searchForm">
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
          >
            <el-form-item label="用户名称:" prop="userName">
              <el-input
                v-model.trim="queryParams.userName"
                placeholder="请输入用户名称"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="手机号码:" prop="phonenumber">
              <el-input
                v-model.trim="queryParams.phonenumber"
                placeholder="请输入手机号码"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="searchButtons">
          <el-button class="searchButton" @click="handleQuery">搜索</el-button>
          <el-button class="resetButton" @click="resetQuery">重置</el-button>
        </div>
      </div>
      <div class="addDeleteBar">
        <el-button
          class="deleteButton"
          @click="openSelectUser"
          v-hasPermi="['system:role:add']"
          ><span class="plus">+</span> 添加用户</el-button
        >
        <el-button
          :class="!multiple ? 'DeleteButton' : 'allDeleteButton'"
          icon="el-icon-delete"
          :disabled="multiple"
          @click="cancelAuthUserAll"
          v-hasPermi="['system:role:remove']"
          >批量取消授权</el-button
        >
      </div>
      <div class="tableSelectArea">
        <div>
          <div class="iconInfo el-icon-info"></div>
          <span class="infoText">已选择</span>
          <span class="infoText infoActive" v-text="selectedNum"></span>
          <span class="infoText">项</span>
          <span class="infoButton" @click="clearSelect">清空</span>
        </div>
        <div>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </div>
      </div>
      <div class="table">
        <el-table
          v-loading="loading"
          ref="multipleTable"
          class="tableArea"
          :data="userList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column
            label="用户名称"
            prop="userName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="用户昵称"
            prop="nickName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="邮箱"
            prop="email"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="手机"
            prop="phonenumber"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.sys_normal_disable"
                :value="scope.row.status"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="left">
            <template slot-scope="scope">
              <div class="tableButtonArea">
                <el-button
                  type="text"
                  class="operationButton"
                  @click="cancelAuthUser(scope.row)"
                  v-hasPermi="['system:role:remove']"
                  >取消授权</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="tableFooter">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <select-user ref="select" :roleId="queryParams.roleId" @ok="handleQuery" />
  </div>
</template>

<script>
import {
  allocatedUserList,
  authUserCancel,
  authUserCancelAll,
} from "@/api/system/role";
import selectUser from "./selectUser";
import { PAGESIZE, PAGENUM, ROLEEXPORTURL } from "../../../utils/variable";
export default {
  name: "AuthUser",
  dicts: ["sys_normal_disable"],
  components: { selectUser },
  data() {
    return {
      activeName: "first",
      selectedNum: 0,
      // 遮罩层
      loading: true,
      // 选中用户组
      userIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: PAGENUM,
        pageSize: PAGESIZE,
        roleId: "",
        userName: "",
        phonenumber: "",
      },
    };
  },
  created() {
    const roleId = this.$route.params && this.$route.params.roleId;
    if (roleId) {
      this.queryParams.roleId = roleId;
      this.getList();
    }
  },
  methods: {
    /** 查询授权用户列表 */
    getList() {
      this.loading = true;
      allocatedUserList(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //清空列表选择
    clearSelect() {
      this.$refs.multipleTable.clearSelection();
    },
    // 返回按钮
    handleClose() {
      const obj = { path: "/system/role" };
      this.$tab.closeOpenPage(obj);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedNum = selection.length;
      this.userIds = selection.map((item) => item.userId);
      this.multiple = !selection.length;
    },
    /** 打开授权用户表弹窗 */
    openSelectUser() {
      this.$refs.select.show();
    },
    /** 取消授权按钮操作 */
    cancelAuthUser(row) {
      const roleId = this.queryParams.roleId;
      this.$modal
        .confirm('确认要取消该用户"' + row.nickName + '"角色吗？')
        .then(function () {
          return authUserCancel({ userId: row.userId, roleId: roleId });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("取消授权成功");
        })
        .catch(() => {});
    },
    /** 批量取消授权按钮操作 */
    cancelAuthUserAll(row) {
      const roleId = this.queryParams.roleId;
      const userIds = this.userIds.join(",");
      this.$modal
        .confirm("是否取消选中用户授权数据项？")
        .then(function () {
          return authUserCancelAll({ roleId: roleId, userIds: userIds });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("取消授权成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../../assets/styles/variables.scss";
.addUser {
}
.app-container {
  height: 100%;
  padding: 0;
  .tableFooter {
    text-align: right;
    justify-content: flex-end;
    // margin-top: 25px;
    margin-right: 35px;
  }
}
//批量删除为选择
.deleteButton {
  border: 1px solid #2985f7;
  border-radius: 2px;
  width: 110px;
  height: 38px;
  background: #598af9;
  font-size: $form-button-font-size;
  font-family: $font-family;
  line-height: 9px;
  font-weight: $form-button-font-weight;
  letter-spacing: 0;
  color: #fff;
  letter-spacing: 0px;
}
//批量删除
.allDeleteButton {
  border: 1px solid $form-button-background-color;
  height: 38px;
  width: 140px;
  border-radius: 2px;
  font-size: $form-button-font-size;
  font-family: $font-family;
  font-weight: $form-button-font-weight;
  letter-spacing: 0;
  color: #598af9;
  line-height: 10px;
  letter-spacing: 0px;
}
.DeleteButton {
  border: 1px solid $form-button-background-color;
  height: 38px;
  width: 140px;
  background: #598af9;
  border-radius: 2px;
  font-size: $form-button-font-size;
  font-family: $font-family;
  font-weight: $form-button-font-weight;
  letter-spacing: 0;
  color: #fff;
  line-height: 10px;
  letter-spacing: 0px;
}
.addButton {
  border: 1px solid $form-button-background-color;
  height: 38px;
  width: 80px;
  text-align: center;
  background: $form-button-background-color;
  border-radius: 2px;
  margin-right: 10px;
  font-size: $form-button-font-size;
  font-family: $font-family;
  font-weight: $form-button-font-weight;
  letter-spacing: 0;
  color: #ffffff;
  line-height: 10px;
  letter-spacing: 1px;
  .plus {
    font-weight: 900;
    font-size: 24px;
  }
}
::v-deep .el-tabs--card .el-tabs__header {
  background-color: #e4e7ed;
}
::v-deep.el-tabs--card .el-tabs__header .el-tabs__item {
  border-radius: 10px solid black;
  background-color: #fff;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #053ef6;
  line-height: 48px;
  margin-top: 0;
}
</style>